<?php
class ModelProductReview extends Model {
	
	public function editReview($review_id, $status) {
		$this->db->query("UPDATE " . DB_PREFIX . "review SET status = '" . (int)$status . 
			"', date_modified = NOW() WHERE review_id = '" . (int)$review_id . "'");
		$this->cache->delete('review');
	}
	
	public function getReview($review_id) {
		$query = $this->db->query("SELECT r.*, p.`name` product_name, c.`firstname`, c.`lastname`, c.`email` FROM " . 
			DB_PREFIX . "review r LEFT JOIN " . 
			DB_PREFIX . "product p ON r.product_id = p.product_id LEFT JOIN " . 
			DB_PREFIX . "customer c ON r.`customer_id` = c.`customer_id` WHERE r.review_id = '" . (int)$review_id . "'");
		return $query->row;
	}

	public function getReviews($data = array()) {
		$sql = "SELECT r.review_id, r.author, r.rating, r.status, r.date_added, p.`name` product_name, pd.display_name product_display_name FROM " . 
			DB_PREFIX . "review r LEFT JOIN " . 
			DB_PREFIX . "product p ON r.product_id = p.product_id LEFT JOIN " . 
			DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id AND pd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "')";																																		  
		if (isset($data['filter_product_display_name'])) {
			$sql .= " WHERE LCASE(pd.display_name) LIKE '" . $this->db->escape($data['filter_product_display_name']) . "%'";	
		}
		$sort_data = array(
			'pd.display_name',
			'r.author',
			'r.rating',
			'r.status',
			'r.date_added'
		);	
		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY r.date_added";	
		}
		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}
		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}			
			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	
			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}																																							  
		$query = $this->db->query($sql);																																				
		return $query->rows;	
	}
	
	public function getTotalReviews($data = array()) {
		$sql = "SELECT COUNT(*) AS total FROM " . 
			DB_PREFIX . "review r LEFT JOIN " . 
			DB_PREFIX . "product p ON r.product_id = p.product_id LEFT JOIN " . 
			DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id AND pd.language_id = '" . 
			(int)$this->config->get('config_language_id') . "')";																																		  
		if (isset($data['filter_product_display_name'])) {
			$sql .= " WHERE LCASE(pd.display_name) LIKE '" . $this->db->escape($data['filter_product_display_name']) . "%'";	
		}
		$query = $this->db->query($sql);
		return $query->row['total'];
	}
	
	public function getTotalReviewsAwaitingApproval() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "review WHERE status = '0'");
		return $query->row['total'];
	}	
}
?>